﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TStocktakeResultRecordRepository : BaseRepository<TStocktakeResultRecord>, ITStocktakeResultRecordRepository
    {
        public TStocktakeResultRecordRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 盘点库显示
        /// </summary>
        /// <param name="wid"></param>
        /// <param name="aid"></param>
        /// <param name="status"></param>
        /// <param name="isHandle"></param>
        /// <param name="orderSN"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TStocktakeResultRecord> GetTStocktakeResultRecordList(int wid, int aid, int status, int isHandle, string orderSN, int pageIndex, int pageSize, out int totalCount)
        {
            var list = Context.TStocktakeResultRecords.AsQueryable();

            //库房
            if(wid!=0)
            {
                list = list.Where(x => x.WarehouseId == wid);
            }
            //库区
            if (aid != 0)
            {
                list = list.Where(x => x.AreaId == aid);
            }
            //状态
            if(status!=0)
            {
                list = list.Where(x => x.Status == status);
            }
            //是否已处理
            if(isHandle<2)
            {
                list = list.Where(x => x.IsHandle == isHandle);
            }
            if(!string.IsNullOrEmpty(orderSN))
            {
                list = list.Where(x => x.OrderSn.Contains(orderSN));
            }
            totalCount = list.Count();

            list = list.OrderByDescending(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list.ToList();
        }

        public bool Tianjiapandianjilu(TStocktakeResultRecorddtofengzhuang dto,out string message)
        {
            using (var reader=Context.Database.BeginTransaction())
            {
                try
                {
                    foreach (var item in dto.Items)
                    {
                        
                        if (string.IsNullOrEmpty(item.pandiandingdan))
                        {
                            message = "返回数据没有值";
                            return false;
                        }
                        if (item.cellcode == null)
                        {
                            message = "货位没有值";
                            return false;
                        }
                        
                        TStocktakeResultRecord xianshi = new TStocktakeResultRecord();
                        xianshi.OrderId = item.OrderId;
                        xianshi.OrderSn = item.pandiandingdan;
                    
                        xianshi.AreaId = item.AreaId;
                        xianshi.AreaName = item.AreaName;
                        xianshi.MaterialId = item.MaterialId;
                        xianshi.MaterialName = item.matername;
                        xianshi.CellCode = item.cellcode;
                        xianshi.BillTypeName = dto.Imgagesrc;
                        xianshi.CellId = item.CellId;
                        xianshi.CellDepthIndex = item.CellDepthIndex;
                        xianshi.CreateUserId = item.CreateUserId;
                        xianshi.OriginalQty = item.qty;//库存数量
                        xianshi.CountQty = item.pty;//盘点后数量
                        if (item.pty==item.qty)//盘平
                        {
                            item.qualityStatus = 1;
                        }
                        else if (item.pty>item.qty)//盘盈
                        {
                            item.qualityStatus = 2; 
                        }
                        else if (item.pty<item.qty)
                        {
                            item.qualityStatus = 3;
                        }
                        xianshi.CreateDate = DateTime.Now;
                        xianshi.Remark = item.Remark;
                        xianshi.Status = item.qualityStatus;
                        xianshi.WarehouseId = item.WarehouseId;
                        xianshi.WarehouseName = item.WarehouseName;
                        xianshi.CreateUserName = dto.CreateName;
                        xianshi.IsHandle=1;
                        xianshi.ScanBarcode = item.supplierName;
                        if (item.qualityStatus == 1)
                        {
                            item.StatusNam = "盘平";
                        }
                        else if (item.qualityStatus == 2)
                        {
                            item.StatusNam = "盘盈";
                        }
                        else if (item.qualityStatus == 3)
                        {
                            item.StatusNam = "盘亏";
                        }
                        Context.TStocktakeResultRecords.Add(xianshi);
                        Context.SaveChanges();
                     
                    }
                    message = "添加成功"+dto.Items.Count;
                    reader.Commit();
                    return true;
                }
                catch (Exception)
                {
                    reader.Rollback();
                    throw new Exception();
                }
            }
        }
    }
}
